<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Redis;

class Mangerislogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('token');
        $memberinfo = Redis::get($token);

        $memberinfo = $memberinfo ? json_decode($memberinfo, true) : '';

        if (!is_array($memberinfo)) {
            return response()->json([
                'code' => 401, 'msg' => '登录失效，请重新登录'
            ], 200);
        } else {

            //判断token是否存在于登录token集合

            
            if (isset($memberinfo['id']) && isset($memberinfo['cpid'])) {
                // $tokenbyid = Redis::get('manger_user_tokenbyid_' . $memberinfo['id']);
                // if ($token!=$tokenbyid) {
                //     return response()->json([
                //         'code' => 401, 'msg' => '登录失效，请重新登录'
                //     ], 200);
                // }
            } else {
                return response()->json([
                    'code' => 401, 'msg' => '登录失效，请重新登录'
                ], 200);
            }

            //是否验证权限，如需校验，获取路由地址，无权限则拦截


            $res = $next($request);
            return $res;
        }
    }
}
